package com.youpai.platform.vulture.service.channel;

import com.alibaba.fastjson.JSONObject;
import com.youpai.platform.vulture.common.entity.AccessMessage;
import io.netty.channel.Channel;

import java.net.InetSocketAddress;
import lombok.extern.slf4j.Slf4j;

/**
 * @date: 2022/7/8 23:27
 * @author: Anna
 * @version: 1.0
 * @description: vulture抽象通道
 */
@Slf4j
public abstract class AbstractVultureChannel implements VultureChannel {

    private final Channel channel;

    private volatile boolean active = false;

    private final long firstConnectTime = System.currentTimeMillis();

    private long lastReceiveHeartbeatTime;

    public AbstractVultureChannel(Channel channel) {
        this.channel = channel;
    }

    @Override
    public InetSocketAddress getLocalAddress() {
        return (InetSocketAddress) channel.localAddress();
    }

    @Override
    public InetSocketAddress getRemoteAddress() {
        return (InetSocketAddress) channel.remoteAddress();
    }

    @Override
    public void send(AccessMessage msg) {
        channel.writeAndFlush(msg);
    }

    @Override
    public Channel getChannel() {
        return channel;
    }

    @Override
    public void makeActive() {
        active = true;
    }

    @Override
    public boolean isActive() {
        return active;
    }

    @Override
    public long getFirstConnectTime() {
        return firstConnectTime;
    }

    @Override
    public long getLastReceiveHeartbeatTime() {
        return lastReceiveHeartbeatTime;
    }

    @Override
    public void refreshLastReceiveHeartbeatTime() {
        lastReceiveHeartbeatTime = System.currentTimeMillis();
    }

    @Override
    public String toString() {
        return "AbstractVultureChannel [ remoteAddr=" + channel.remoteAddress() + "localAddr=" + channel.localAddress()
                + ']';
    }
}
